

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class Window extends JFrame implements ActionListener, WindowListener,ChangeListener{

	//public JPanel simulationArea;
	private JRadioButton jrAnthill, jrFeed, jrBarricades;
	private JSlider jsFeed, jsAnts;
	private JButton jbStart, jbBreak, jbContinue, jbNew,jbSettings, jbExit;
	private final 	Insets defaultIns = new Insets(2,2,2,2);
	
	private Simulation simulationThread;
	
	//JSlider finals
	public static final int ANTS_MIN=160,ANTS_MAX=1760,ANTS_INIT=960,MINOR_TICK=80,MAJOR_TICK=400; 
	public static final int FEED_MIN=20,FEED_MAX=100,FEED_INIT=60, FEED_MAJOR_TICK=20,FEED_MINOR_TICK=5;
	
	private AntSimulation antSim;
	
	public Window(AntSimulation as){
		super("Ant Simulation");
	
		antSim=as;
		int w=antSim.getSimWidth();
		int h=antSim.getSimHeight();
		
		this.simulationThread=new Simulation(antSim);
		
		GridBagLayout gb = new GridBagLayout();
			this.setLayout(gb);
		GridBagConstraints c = new GridBagConstraints();
		
			c.insets =  defaultIns;
			c.anchor=GridBagConstraints.FIRST_LINE_START;
			
		JLabel jlForest = new JLabel("forest ground");
				gb.setConstraints(jlForest, c);
			this.add(jlForest);
			
		//simulationBG = new JPanel();
//		AntSimulation.simulationArea = new JPanel(true)	{
//				private static final long serialVersionUID = 1L;
//				protected void paintComponent( Graphics g ){
//								
//					super.paintComponent( g );
//					
//					if(AntSimulation.started){
//						AntSimulation.draw( g );
//					}
//					
//				}
//			};
				antSim.getSimArea().setBackground(AntSimulation.bgColor);
				antSim.getSimArea().setPreferredSize(new Dimension(w,h));
				//simulationArea.setBorder(BorderFactory.createLoweredBevelBorder());
		/*	simulationArea =new SimulationArea();
			simulationArea.setPreferredSize(new Dimension(w,h));
			simulationArea.setBorder(BorderFactory.createLoweredBevelBorder());*/
					c.gridheight=9;
					c.gridy=1;
					gb.setConstraints(antSim.getSimArea(), c);
			this.add(antSim.getSimArea());
		
		JLabel jlInputMode = new JLabel("input mode:");
				c.gridx=1;
				c.gridheight=1;
				gb.setConstraints(jlInputMode, c);
			this.add(jlInputMode);
			
		jrAnthill = new JRadioButton("anthill",true);
				c.gridy=2;
				gb.setConstraints(jrAnthill, c);
			this.add(jrAnthill);
			
		jrFeed = new JRadioButton("feed");
				c.gridy=3;
				gb.setConstraints(jrFeed, c);
			this.add(jrFeed);
			
		jrBarricades = new JRadioButton("barricades");
				c.gridy=4;
				gb.setConstraints(jrBarricades, c);
			this.add(jrBarricades);	
	
			ButtonGroup bgControl = new ButtonGroup();		
				bgControl.add(jrAnthill);
				bgControl.add(jrFeed);
				bgControl.add(jrBarricades);
			
			
		JLabel jlSizeFeed = new JLabel("size of feed depot:");
				c.gridy=5;
				c.gridwidth=2;
				//c.insets = new Insets(10,2,2,2);
				gb.setConstraints(jlSizeFeed, c);
			this.add(jlSizeFeed);	
			
		jsFeed = new JSlider(FEED_MIN,FEED_MAX,FEED_INIT);
				jsFeed.setMajorTickSpacing(FEED_MAJOR_TICK);
				jsFeed.setMinorTickSpacing(FEED_MINOR_TICK);
				jsFeed.setPaintLabels(true);
				jsFeed.setPaintTicks(true);
				jsFeed.setSnapToTicks(true);
				jsFeed.addChangeListener(this);
					c.gridy=6;
					c.insets= defaultIns;
					gb.setConstraints(jsFeed, c);
			this.add(jsFeed);
			
		JLabel jlNumberAnts = new JLabel("total number of ants:");
				c.gridy=7;
				gb.setConstraints(jlNumberAnts, c);
			this.add(jlNumberAnts);
			
		jsAnts = new JSlider(ANTS_MIN,ANTS_MAX,ANTS_INIT);
				jsAnts.setMajorTickSpacing(MAJOR_TICK);
				jsAnts.setMinorTickSpacing(MINOR_TICK);
				jsAnts.setPaintTicks(true);
				jsAnts.setPaintLabels(true);
				jsAnts.setSnapToTicks(true);
				jsAnts.addChangeListener(this);
					c.gridy=8;
					gb.setConstraints(jsAnts, c);
			this.add(jsAnts);	
		
		JPanel jpButtons = new JPanel();
			makeButtonPanel(jpButtons);
					c.gridy=9;
					c.anchor=GridBagConstraints.PAGE_END;
					gb.setConstraints(jpButtons, c);
				this.add(jpButtons);
			
		this.setResizable(false);		
		this.addWindowListener(this);
		this.setSize(new Dimension(w+250,h+75))	;
		this.setVisible(true);
	
	}//constructor
	
	private void makeButtonPanel(JPanel jpButtons){
		
		Dimension dButton = new Dimension(100,20);
		
		GridBagLayout gbButtons = new GridBagLayout();
			jpButtons.setLayout(gbButtons);
		GridBagConstraints cButtons = new GridBagConstraints();
			cButtons.insets = defaultIns;
			cButtons.anchor=GridBagConstraints.LINE_START;
			
		jbStart = new JButton("start");
			jbStart.setPreferredSize(dButton);
			jbStart.addActionListener(this);
					gbButtons.setConstraints(jbStart, cButtons);
				jpButtons.add(jbStart);
			
		jbBreak = new JButton("break");	
			jbBreak.setPreferredSize(dButton);
			jbBreak.setEnabled(false);
			jbBreak.addActionListener(this);
					cButtons.gridy=1;
					gbButtons.setConstraints(jbBreak,cButtons);
				jpButtons.add(jbBreak);
			
		jbContinue = new JButton("continue");	
			jbContinue.setPreferredSize(dButton);
			jbContinue.setEnabled(false);
			jbContinue.addActionListener(this);
					cButtons.gridx=1;
					gbButtons.setConstraints(jbContinue,cButtons);
				jpButtons.add(jbContinue);		
		
		jbNew = new JButton("new");	
			jbNew.setPreferredSize(dButton);
			jbNew.addActionListener(this);
					cButtons.gridy=2;
					cButtons.gridx=0;
					//cButtons.insets=new Insets(20,2,2,2);
					gbButtons.setConstraints(jbNew,cButtons);
				jpButtons.add(jbNew);
		
		jbSettings = new JButton("reset");	
			jbSettings.setPreferredSize(dButton);
			jbSettings.addActionListener(this);
					cButtons.gridx=1;
					//cButtons.insets=new Insets(20,2,2,2);
					gbButtons.setConstraints(jbSettings,cButtons);
				jpButtons.add(jbSettings);				
				
		jbExit = new JButton("exit");
			jbExit.setPreferredSize(dButton);
			jbExit.addActionListener(this);
					cButtons.gridy=3;
					//cButtons.gridx=1;
					gbButtons.setConstraints(jbExit,cButtons);
				jpButtons.add(jbExit);		
				
	}//makeButtonPanel

	/*public JPanel getSimArea(){
		return simulationArea;
	}*/
	
	/*
	 * getter
	 */
	public int getNumberOfAnts(){
		return jsAnts.getValue();
	}
	
	public int getSizeFeed(){
		return jsFeed.getValue();
	}
	
	public boolean getFeed(){
		return jrFeed.isSelected();
	}
	
	public boolean getAnthill(){
		return jrAnthill.isSelected();
	}
	
	public boolean getBarricades(){
		return jrBarricades.isSelected();
	}
	
	//*****NEW**********little Helper
	public void guiEnabler(boolean enabled){
		jbContinue.setEnabled(enabled);
		jbNew.setEnabled(enabled);
		jbSettings.setEnabled(enabled);
		jbBreak.setEnabled(!enabled);
	}//guiEnabler()
	
	public void actionPerformed(ActionEvent aEv) {
		Object source = aEv.getSource();
		
		if(source==jbStart){
			
			if(antSim.isCorrectlySet()){
				antSim.setStarted(true);
			
			//gui handler
			jbStart.setEnabled(false);
				guiEnabler(false);
			
			jsAnts.setEnabled(false);
			jsFeed.setEnabled(false);
			
			antSim.initialzeSimulation();

			simulationThread.start_new_simulation();
			}
			else{
				System.out.println("Missing Information. Please set feed and anthill correctly" +
						"\n-they should not interlapse or be out of the simulation box.");
			}

		}else if(source==jbBreak){
			
			guiEnabler(true);
			
			simulationThread.interrupt_simulation();
			
		}else if(source==jbContinue){
			
			guiEnabler(false);
			
			simulationThread.continue_simulation();
			
		}else if(source==jbNew){	
			
			simulationThread.exit_Simulation();
		
			antSim.getWindow().dispose();
			Start.startProgram();
			
		}else if(source==jbSettings){
		
			simulationThread.exit_Simulation();
			
			jbStart.setEnabled(true);
			jbContinue.setEnabled(false);
			jbBreak.setEnabled(false);
			
			jsAnts.setEnabled(true);
			jsFeed.setEnabled(true);
			
			antSim.newSettings();
			
		}else if(source==jbExit){
		
			simulationThread.exit_Simulation();
			
			System.exit(0);
		}
		
	}//actionPerformed(ActionEvent aEV)

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		antSim.setWasMinimized(true);
	//	antSim.revalidateForestGround();
		System.out.println("deicon");
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void stateChanged(ChangeEvent cEv) {
		Object source=cEv.getSource();
		if(source ==jsAnts){
			antSim.updateRectangle(1);
		}
		else if(source == jsFeed){
			antSim.updateRectangle(2);
		}
		
	}
	


}//class Window
